<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="productList">
    <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="供应商" align="center" prop="supplyName" width="150"/>
      <el-table-column label="商品码" align="center" prop="productCode" width="150"/>
      <el-table-column label="商品名称" align="center" prop="productName" width="150"/>
        <el-table-column label="规格" align="center" prop="unit" width="80"/>
      <el-table-column
        label="生产日期"
        align="center"
        prop="manufactureDate"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.manufactureDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过期日期"
        align="center"
        prop="expirationDate"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="categoryName" width="80"/>
         <el-table-column label="建议采价" align="center" prop="cjPrice" width="80"/>
      <el-table-column label="建议销价" align="center" prop="xsPrice" width="80"/>
      <el-table-column label="产地" align="center" prop="countryOfOrigin" width="100"/>
      <el-table-column label="商品品牌" align="center" prop="productBrand" width="150"/>
      <el-table-column label="审核状态" align="center" prop="isSh" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isSh === '0'">已通过</el-tag>
          <el-tag type="info" v-if="scope.row.isSh === '1'">待审核</el-tag>
          <el-tag type="danger" v-if="scope.row.isSh === '2'">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" width="80"/>
      <el-table-column
        label="操作"
          fixed="right"
      width="100"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supermarket:product:sh']"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品码" prop="productCode">
              <el-input
                v-model="form.productCode"
                placeholder="请输入商品码"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品品牌" prop="productBrand">
              <el-input
                v-model="form.productBrand"
                placeholder="请输入商品品牌"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="商品名称" prop="productName">
            <el-input
              v-model="form.productName"
              placeholder="请输入商品名称"
              :disabled="true"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类" prop="categoryId">
              <treeselect
                v-model="form.categoryId"
                :options="categoryOptions"
                :normalizer="normalizer"
                placeholder="请选择分类"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplyId">
              <treeselect
                v-model="form.supplyId"
                :options="supplyOptions"
                :normalizer="normalizer1"
                placeholder="请选择供客"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="规格" prop="unit">
              <el-input
                v-model="form.unit"
                placeholder="请输入规格"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产地" prop="countryOfOrigin">
              <el-input
                v-model="form.countryOfOrigin"
                placeholder="请输入产地"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="manufactureDate">
              <el-date-picker
                clearable
                v-model="form.manufactureDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择生产日期"
                :disabled="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="过期日期" prop="expirationDate">
              <el-date-picker
                clearable
                v-model="form.expirationDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择过期日期"
                :disabled="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="建议采购价" prop="cjPrice">
              <el-input
                v-model="form.cjPrice"
                placeholder="请输入建议采购价"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建议销售价" prop="xsPrice">
              <el-input
                v-model="form.xsPrice"
                placeholder="请输入建议销售价"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="审核意见">
              <el-input
                v-model="form.shRemark"
                type="textarea"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(0)">通 过</el-button>
        <el-button type="primary" @click="submitForm(2)">不通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
  shProduct,
  listShProduct,
} from "@/api/supermarket/product";
import { listCategory } from "@/api/supermarket/category";
import { listSupplyGK } from "@/api/supermarket/supply";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Product",
  dicts: ["sys_normal_disable"],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productList: [],
      // 商品分类表格数据
      categoryOptions: [],
      //供客
      supplyOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: null,
        productName: null,
        productBrand: null,
        cjPrice: null,
        xsPrice: null,
        unit: null,
        manufactureDate: null,
        expirationDate: null,
        countryOfOrigin: null,
        categoryId: null,
        supplyId: null,
        stockId: null,
        isSh: null,
        status: null,
        createName: null,
        updateName: null,
        shRemark: null,
      },
      // 表单参数
      form: {
      },
            rules: {
        productCode: [
          { required: true, message: "商品码不能为空", trigger: "blur" },
        ],
        productName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        unit: [{ required: true, message: "规格不能为空", trigger: "blur" }],
        manufactureDate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" },
        ],
        expirationDate: [
          { required: true, message: "过期日期不能为空", trigger: "blur" },
        ],
        categoryId: [
          { required: true, message: "分类不能为空", trigger: "blur" },
        ],
        supplyId: [
          { required: true, message: "供应商不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listShProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 转换分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.categoryId,
        label: node.categoryName,
        children: node.children,
      };
    },
    normalizer1(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.supplyId,
        label: node.supplyName,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productCode: null,
        productName: null,
        productBrand: null,
        cjPrice: null,
        xsPrice: null,
        unit: null,
        manufactureDate: null,
        expirationDate: null,
        countryOfOrigin: null,
        categoryId: null,
        supplyId: null,
        stockId: null,
        isSh: null,
        status: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createName: null,
        updateName: null,
        shRemark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handFG() {
       listCategory().then((response) => {
        this.categoryOptions = this.handleTree(
          response.data,
          "categoryId",
          "fcategoryId"
        );
      });
      const type = 1;
      listSupplyGK(type).then((response) => {
        this.supplyOptions = this.handleTree(response.data, "supplyId");
      });
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
      this.handFG();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.handFG();
      const productId = row.productId;
      getProduct(productId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "审核商品";
      });
    },
    /** 提交按钮 */
    submitForm(i) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (i === 0) {
            this.form.isSh = '0'
            shProduct(this.form).then((response) => {
            this.$modal.msgSuccess("审核通过");
            this.open = false;
            this.getList();
          });
          } else {
            this.form.isSh = '2'
            shProduct(this.form).then((response) => {
            this.$modal.msgError("审核不通过");
            this.open = false;
            this.getList();
          });
          }
          
        }
      });
    },
  },
};
</script>
<style>
.el-input input:disabled {
  color: #000 !important; /* 设置为灰色 */
}

.el-textarea__inner {
  color: #000 !important;
}
</style>